using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Text;

//using iTunesLib;
//using itunes2;

namespace WindowsApplication1
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
	/*	[ DllImport("user32.dll") ]
		static extern IntPtr GetForegroundWindow();

		[ DllImport("Psapi.dll") ]
			static extern int GetModuleFileNameEx(IntPtr hProcess, IntPtr hModule, StringBuilder text, int count); 
		//[ DllImport("user32.dll") ]
		//static extern Int32 GetForegroundWindow();


		[ DllImport("user32.dll") ]
		static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
		
		[DllImport("user32.dll")]
		static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId);

		[ DllImport("user32.dll") ]
		static extern int GetWindowModuleFileName(IntPtr hWnd, StringBuilder text, int count);
		
		[DllImport("user32")]
		private static extern UInt32 GetWindowThreadProcessId(
			Int32 hWnd,
			out Int32 lpdwProcessId
			);
		public static Int32 GetWindowProcessID(Int32 hwnd)
		{
			Int32 pid = 1;
			GetWindowThreadProcessId(hwnd, out pid);
			return pid;
		}
		/*
		[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)]
		public static extern IntPtr GetParent(IntPtr hWnd);
		

		[DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)] 
		public static extern IntPtr GetWindow(IntPtr hWnd, int uCmd); 

		[DllImport("user32.Dll")]
		public static extern void GetClassName(int h, StringBuilder s, int nMaxCount);

		[DllImport("user32.dll")]
		static extern bool IsChild(IntPtr hWndParent, IntPtr hWnd);
		/*
		[DllImport("user32.dll")] private static extern 
			IntPtr GetForegroundWindow();
		[DllImport("user32.dll")] private static extern 
			int GetWindowText(int hWnd, StringBuilder title, int size);*/

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Timer timer1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.ComponentModel.IContainer components;
//		private itunes2.LCDSmartie lcd;
        private vlc.LCDSmartie Vlc,lcd;
		//private processes.LCDSmartie proc;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
		//	lcd = new itunes2.LCDSmartie();
//			proc = new processes.LCDSmartie();

            Vlc = new vlc.LCDSmartie();
            lcd = Vlc;


            timer1.Interval = lcd.GetMinRefreshInterval();
            timer1.Enabled = true;
            /*
						foreach (IITTrack track in app.LibraryPlaylist.Tracks)

						{

							label2.Text += track.ToString();

						}
			*/			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            this.label1 = new System.Windows.Forms.Label();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.button1 = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(313, 112);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(251, 285);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(104, 368);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(48, 32);
            this.button1.TabIndex = 1;
            this.button1.Text = "button1";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(8, 65);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(248, 287);
            this.label2.TabIndex = 2;
            this.label2.Text = "label2";
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(8, 16);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(528, 208);
            this.label3.TabIndex = 3;
            this.label3.Text = "label3";
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(576, 406);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label3);
            this.Name = "Form1";
            this.Text = "Erlis";
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			float vol = ((float)AudioMixerHelper.GetVolume()/(float)0xffff);
			
			label1.Text = ((float)AudioMixerHelper.GetVolume()/(float)0xffff).ToString("##0%");

    		label2.Text = lcd.function1("Stat","") + "\r\n";
			label2.Text +=	lcd.function1("Filename","")+ "\r\n";
			label2.Text +=	lcd.function1("Channels","")+ "\r\n";
            label2.Text += lcd.function1("Chapter", "") + " / "; 
            label2.Text += lcd.function1("ChapterTot", "") + "\r\n";
			label2.Text +=	lcd.function1("KBPS","")+ "\r\n";
			label2.Text +=	lcd.function1("Freq","")+ "\r\n";
			label2.Text +=	lcd.function1("Length","")+ "|";
			label2.Text +=	lcd.function1("Lengtl","")+ "|";
			label2.Text +=	lcd.function1("Lengts","")+ "\r\n";
			label2.Text +=	lcd.function1("pos","")+ "|";
			label2.Text +=	lcd.function1("Posh","")+ "|";
			label2.Text +=	lcd.function1("Polo","")+ "\r\n";
			label2.Text +=	lcd.function1("Rem","")+ "|";
			label2.Text +=	lcd.function1("Resh","")+ "|";
			label2.Text +=	lcd.function1("Relo","")+ "\r\n";
			label2.Text +=	lcd.function1("Position","25")+ "\r\n";
			label2.Text +=	lcd.function1("Tracknr","")+ "\r\n";
			label2.Text +=	lcd.function1("TotalTracks","")+ "\r\n";
			label2.Text +=	lcd.function1("PlayList","")+ "\r\n";
            label2.Text += Vlc.function1("Time", "");
            label2.Text += Vlc.function1("All", "");

           this.Text = lcd.function1("Title","20");

/*			label3.Text = this.proc.function1("","");
			label3.Text += "\r\n" + this.proc.function2("iTunes","");
			label3.Text += "\r\n" + this.proc.function3("","");
			label3.Text += "\r\n" + this.proc.function4("","");
			label3.Text += "\r\n" + this.proc.function5("firefox","");
			/*label3.Text = this.GetCurrentWindowTitle();
			label3.Text += "\r\n" + this.GetCurrentProcess();
			label3.Text += "\r\n" + this.ProcessActive("devenv").ToString();
*/
			//	label3.Text += "\r\n" + this.ProcessChild("devenv").ToString();
						
		/*	IntPtr handle;

			handle = GetWindowThreadProcessId( GetForegroundWindow(), IntPtr.Zero);
			//Process myProcess = Process.GetProcessById(handle.ToInt32());
			/*Process[] myProcesses = Process.GetProcesses();
			foreach(Process myProcess in myProcesses)
				label3.Text += myProcess.Id.ToString() + ";";
			label3.Text += "\r\n" + handle.ToString(); //myProcess.ProcessName.ToString();
			*/
/*			StringBuilder Buff = new StringBuilder(256);			
			GetWindowModuleFileName(GetForegroundWindow(),Buff,256);
			GetModuleFileNameEx(GetForegroundWindow(), IntPtr.Zero,Buff,256);
			label3.Text += "\r\n" + Buff.ToString();
			label3.Text += "\r\n" + (Process.GetProcessById(GetWindowProcessID(GetForegroundWindow().ToInt32()))).ProcessName; //Buff.ToString();
*/		}
	/*	private string GetCurrentWindowTitle()
		{
			try 
			{
				IntPtr handle;
				StringBuilder Buff = new StringBuilder(256);
				handle = GetForegroundWindow();
				if ( GetWindowText(handle, Buff, 256) > 0 )
					return Buff.ToString();
			}
			catch (Exception) {;}
			return "";
		}

		private string GetCurrentProcess()
		{
			try 
			{
				string windowTitle = GetCurrentWindowTitle();
				if(windowTitle.Length > 0)
				{
					Process[] myProcesses = Process.GetProcesses();
					foreach(Process myProcess in myProcesses)
						if(myProcess.MainWindowTitle.Length > 0)
							if(myProcess.MainWindowTitle.Equals(windowTitle))
								return myProcess.ProcessName;
				}
			}
			catch (Exception) {;}
			return "";
		}
		
		private int ProcessActive(string process)
		{
			try 
			{
				string windowTitle = GetCurrentWindowTitle();
				if(windowTitle.Length > 0)
				{
					Process[] myProcesses = Process.GetProcessesByName(process);
					foreach(Process myProcess in myProcesses)
						if(myProcess.MainWindowTitle.Length > 0)
							if(myProcess.MainWindowTitle.Equals(windowTitle))
								return 0;
				}
			} 
			catch (Exception) {;}
			return 1;
		}
	/*	private bool ProcessChild(string process)
		{
			try 
			{
				string windowTitle = GetCurrentWindowTitle();
				if(windowTitle.Length > 0)
				{
					Process[] myProcesses = Process.GetProcessesByName(process);
					foreach(Process myProcess in myProcesses)
					{
						IntPtr fg = GetForegroundWindow();
						while(fg != (IntPtr)0)
						{
							
							if(myProcess.MainWindowHandle == fg)
								return true;
							fg = GetParent(fg);
						}
						//if(myProcess.MainWindowTitle.Length > 0) 
						if(IsChild(myProcess.Handle, GetForegroundWindow()))
							return true;
						if(IsChild(myProcess.MainWindowHandle, GetForegroundWindow()))
							return true;
						if(IsChild(GetForegroundWindow(), myProcess.MainWindowHandle))
							return true;
						//if(IsChild(myProcess. GetForegroundWindow()))
						//	return true;
					}
					}
			} 
			catch (Exception) {;}
			return false;
		}*/
	/*	private string sec2min(int sec)
		{
			int minutes = (sec / 60);
			int seconds = sec-(minutes*60);
			return minutes.ToString("#0") + ":"+ seconds.ToString("00");
		}
	*/	private void button1_Click(object sender, System.EventArgs e)
		{
			AudioMixerHelper.SetVolume(0xffff/2);
		}

/*		private void GetActiveWindow()
		{

			const int nChars = 256;
			int handle = 0;
			StringBuilder Buff = new StringBuilder(nChars);
   
			handle = GetForegroundWindow();
			//Process p = Process.GetProcessById(handle);
			//label3.Text = p.MainWindowTitle;
			
			if ( GetWindowText(handle, Buff, nChars) > 0 )
			{
				label3.Text +=  Buff.ToString();
				//label3.Text = handle.ToString();
			}

		}
*/
	}
}

